package es.xpt.ysf.commons.jaas.view;

import java.util.List;
import java.util.TreeSet;

import es.xpt.ysf.YSFException;
import es.xpt.ysf.commons.Names;
import es.xpt.ysf.commons.jaas.JaasSession;
import es.xpt.ysf.jaas.YSFPrincipal;
import es.xpt.ysf.model.Item;
import es.xpt.ysf.view.LocalDelegate;
import es.xpt.ysf.view.model.UserBean;

public class JaasDelegate extends LocalDelegate<JaasSession> {

	public JaasDelegate(UserBean user) {
		super(user);
	}

	public List<YSFPrincipal> getEntityUsers(String entity, String entityId)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		return ejb.getEntityUsers(entity, entityId);
	}

	public void insertUser(YSFPrincipal principal, String pass, List<Item> roles,
			String entityId, String entity) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.insertUser(principal, pass, roles, entityId, entity);
	}

	public void deleteUser(String entityId, String entity, String principal)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.deleteUser(entityId, entity, principal);
	}

	public void updateUser(YSFPrincipal principal, String pass, List<Item> roles)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.updateUser(principal, pass, roles);
	}

	
// --------------------- ROLES ------------------------
	public List<Item> getRoles() throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		return ejb.getRoles();
	}

	public List<Item> getUserRoles(String principal) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		return ejb.getUserRoles(principal, null);
	}
	
	public TreeSet<String> getRolePrivileges(String role) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		return ejb.getRolePrivileges(role);
	}

	public void deleteRole(String role) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.deleteRole(role);
	}
	public void insertRole(Item role) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.insertRole(role);
	}
	public void updateRole(Item role) throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.updateRole(role);
	}
	
	public void removePrivilege(String role, String privilege)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.removePrivilege(role, privilege);
	}

	public void removePrivileges(String role, String[] privileges)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.removePrivileges(role, privileges);
	}

	public void insertPrivileges(String role, String[] privileges)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.insertPrivileges(role, privileges);
	}

	public void insertPrivilege(String role, String privilege)
			throws YSFException {
		JaasSession ejb = connect(Names.JAAS);
		ejb.insertPrivilege(role, privilege);
	}

}
